package es.onlysolutions.gs.web.tag;

import es.onlysolutions.gs.comm.utils.UserSettings;
import es.onlysolutions.gs.comm.entity.User;
import es.onlysolutions.arq.core.auth.IUserSettings;

import javax.servlet.jsp.JspException;
import java.io.IOException;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

/**
 * This tag try to get the User in session and print a readable String with the user login.
 */
public class ShowUserTag extends GSDynamicTag
{

    /**
     * The logger for the class.
     */
    private static final Log logger = LogFactory.getLog(GSDynamicTag.class);

    public int doStartTag() throws JspException
    {
        String outPutString = "";//Empty String if we cannot get the user.
        UserSettings userSettings = (UserSettings) pageContext.getSession().getAttribute(IUserSettings.USER_SETTINGS_ATTRIBUTE_NAME);

        if( userSettings != null )
        {
            User user = userSettings.getUser();
            if( user != null )
            {
                outPutString = user.getLogin();
            }
        }

        try
        {
            write( outPutString.trim() );
        }
        catch (IOException e)
        {
            logger.error(e);
            throw new JspException(e);
        }

        return SKIP_BODY;
    }
}
